<?php
namespace App\Admin\Controller;

use Foxforms\Widget\Field\Radio;

use Entities\Corp\Aplicacao;
use Foxforms\Widget\Field\Input;
use Foxforms\Widget\Form;

class aplicacaoController extends \Foxframe\Controller
{
	public function index($params = array())
	{
		$this->redirect(BASE_PATH.'admin/aplicacao/viewListar');
	}

	public function viewListar($params = array())
	{
		$lista = $this->em->getRepository('Entities\Corp\Aplicacao')->listar();
		$this->view->assign('lista', $lista);
	}

	public function viewCadastrar($params = array())
	{
		if (isset($params['id_aplicacao']) && !empty($params['id_aplicacao']))
			$aplicacao	= $this->em->find('\Entities\Corp\Aplicacao', $params['id_aplicacao']);
		else
			$aplicacao	= new \Entities\Corp\Aplicacao();

		$form	= new Form();
		$form->addValues($aplicacao, 'aplicacao');
		$form->addClass('validate');
		$form->addElement(Input::create('application')->setType('hidden')->setValue('admin'));
		$form->addElement(Input::create('controller')->setType('hidden')->setValue('aplicacao'));
		$form->addElement(Input::create('method')->setType('hidden')->setValue('saveAplicacao'));
		$form->addElement(Input::create('aplicacao[version]')->setType('hidden'));
		$form->addElement(Input::create('aplicacao[id_aplicacao]')->setType('hidden'));
		$form->addElement(Input::create('aplicacao[ds_nome]')->setLabel('Descrição')->validate()->addClass('input-xlarge'));
		$form->addElement(Input::create('aplicacao[ds_path]')->setLabel('Path')->validate()->addClass('input-xlarge'));
		$form->addElement(Input::create('aplicacao[ds_observacao]')->setLabel('Observação')->addClass('input-xlarge'));
		$form->addElement(
				Radio::create('aplicacao[fl_status]')
				->setLabel('Status')
				->setOption(array(1 => 'Ativo', 0 => 'Inativo'))
				->validate()
		);
		$form->addElement(Input::create('salvar')->setType('submit')->setValue('Salvar')->addClass(array('btn','btn-primary')));
		$this->view->assign('form', $form);

		$this->returnAjax();
	}

	public function saveAplicacao($params = array())
	{
		try
		{
			$service	= new \Foxframe\Service();
			$aplicacao		= $service->save($params['aplicacao'], '\Entities\Corp\Aplicacao');
			$this->em->flush();

			$this->setFlash('Success', 'Sucesso', 'Aplicacao salva com sucesso');
			$this->redirect(BASE_PATH.'admin/aplicacao');
		}
		catch (Exception $e)
		{
			$this->setFlash('Error', 'Erro ao salvar aplicacao', $e->getMessage());
		}
	}

	public function returnController($params = array())
	{
		$aplicacao	= $this->em->find('\Entities\Corp\Aplicacao', $params['id']);
		$retorno	= array();
		if ($aplicacao instanceof \Entities\Corp\Aplicacao)
		{
			$dir	= APP_PATH.ucfirst($aplicacao->getDsPath()).'/Controller';
			$handle	= opendir($dir);


			while ($file = readdir($handle))
			{
				if (is_file($dir.'/'.$file))
				{
					$name			= str_replace('Controller.php', '', $file);
					$retorno[$name]	= ucfirst($name);
				}
			}
			closedir($handle);
			asort($retorno);
		}
		echo json_encode($retorno);
		die;
	}

	public function returnMethod($params = array())
	{
		$aplicacao	= $this->em->find('\Entities\Corp\Aplicacao', $params['idaplicacao']);
		$retorno	= array();
		if ($aplicacao instanceof \Entities\Corp\Aplicacao)
		{
			$superMethods	= get_class_methods('\\Foxframe\\Controller');

			$controller	= '\\App\\'.ucfirst($aplicacao->getDsPath()).'\\Controller\\'.$params['ds_controller'].'Controller';
			$methods	= get_class_methods($controller);

			foreach ($methods as $method)
			{
				//REMOVENDO METHODS DA CLASSE PAI
				if (!in_array($method, $superMethods)
					&& strpos($method, 'view') !== false)
				{
					$name				= str_replace('view', '', $method);
					$retorno[$method]	= $name;
				}
			}
			asort($retorno);
		}
		echo json_encode($retorno);
		die;
	}
}